﻿namespace Sitecore.ChangesTracker.Tasks
{
  using System;

  using Configuration;
  using Data;
  using Data.Archiving;
  using Diagnostics;
  using Archiving;
  using Sitecore.Configuration;


  public class ChangeTrackerCleaner
  {
    private int daystokeep;

    public ChangeTrackerCleaner(string daysToKeep)
    {
      Assert.ArgumentNotNullOrEmpty(daysToKeep, "DaysToKeep");
      this.daystokeep = Convert.ToInt32(daysToKeep);
    }

    public void Run()
    {
      ArchiveProvider provider = ArchiveManager.Providers["changetracker"];
      ArchiveQuery query = new ArchiveQuery();
      query.ArchiveDateEnd = DateTime.Today.AddDays(-this.daystokeep);
      foreach (Database d in Factory.GetDatabases())
      {
        Archive archive = provider.GetArchive("changetracker", d);
        if (archive != null)
        {
          ((ChangeTrackerArchive)archive).RemoveEntries(query);
        }
      }
    }
  }
}

